package com.free.mysql.util;

import com.free.mysql.model.Column;

public class Utils {

    public static String colName(String columnName) {
        int index = columnName.indexOf('_');
        if (index >= 0) {
            String[] arr = columnName.split("_");
            String colName = null;
            for (int i = 0, len = arr.length; i < len; i++) {
                if (i == 0) {
                    colName = new String(arr[0]);
                } else {
                    colName += firstUpper(arr[i]);
                }
            }
            return colName;
        } else {
            return columnName;
        }
    }


    public static String javaType(String typeName, Column column) {
        if ("VARCHAR".equals(typeName)) {
            return "java.lang.String";
        } else if ("CHAR".equals(typeName)) {
            return "java.lang.String";
        } else if ("INT".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("DOUBLE".equals(typeName)) {
            return "java.lang.Double";
        } else if ("INTEGER".equals(typeName)) {
            return "java.lang.Long";
        } else if ("DATE".equals(typeName)) {
            column.setIsNullable(1);
            return "java.util.Date";
        } else if ("BIGINT".equals(typeName)) {
            return "java.lang.Long";
        } else if ("FLOAT".equals(typeName)) {
            return "java.lang.Float";
        } else if ("TEXT".equals(typeName)) {
            return "java.lang.String";
        } else if ("DECIMAL".equals(typeName)) {
            return "java.math.BigDecimal";
        } else if ("TIME".equals(typeName)) {
            column.setIsNullable(1);
            return "java.util.Date";
        } else if ("BLOB".equals(typeName)) {
            return "java.lang.byte[]";
        } else if ("DATETIME".equals(typeName)) {
            return "java.util.Date";
        } else if ("MEDIUMINT".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("TINYINT".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("SMALLINT".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("BIGINT UNSIGNED".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("INTEGER UNSIGNED".equals(typeName)) {
            return "java.lang.Long";
        } else if ("TINYINT UNSIGNED".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("SMALLINT UNSIGNED".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("MEDIUMINT UNSIGNED".equals(typeName)) {
            return "java.lang.Integer";
        } else if ("PK (INTEGER UNSIGNED)".equals(typeName)) {
            return "java.lang.Long";
        } else if ("BIT".equals(typeName)) {
            return "java.lang.Boolean";
        } else if ("PK".equals(typeName)) {
            return "java.lang.Long";
        } else if ("TIMESTAMP".equals(typeName)) {
            return "java.util.Date";
        } else {
            return "java.lang.String";
        }
    }

    public static String firstUpper(String colName) {
        return colName.substring(0, 1).toUpperCase() + colName.substring(1);
    }
}
